<?php
/**
 * Zendex (Zend (Framework) Extensions)
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://zfx.netatoo.fr/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to support@netatoo.fr so we can send you a copy immediately.
 *
 * @category   Zendex
 * @package    Zendex_Bootstrap
 * @copyright  Copyright (c) 2007 Netatoo FR S.A.R.L. (http://www.netatoo.fr)
 * @license    http://Zendex.netatoo.fr/license/new-bsd     New BSD License
 */

/**
 * @category   Zendex
 * @package    Zendex_Bootstrap
 * @copyright  Copyright (c) 2007 Netatoo FR S.A.R.L. (http://www.netatoo.fr)
 * @license    http://Zendex.netatoo.fr/license/new-bsd     New BSD License
 */
class Zendex_Bootstrap_Plugin_Xpath extends Zendex_Bootstrap_Plugin_Abstract
{
    
    /**
     * Here, we quickly parse the XML file to look for calls to XPATH.
     * If XPATH ago, we run self::_xpath() which is designed to recover 
     * regexp masks for SimpleXMLElement::xpath().
     * The replacement of the data is done via str_replace.
     *
     * @param SimpleXMLElement $xml
     */
    public function launchStartup()
    {
        $document = $this->getXmlDocument();
        $asXml = $document->asXml();

        if(strpos($asXml, '$XPATH.XML{')) {
            Zend_Debug::dump('$XPATH.XML{}');
            $this->_xpath('XML');
        } 
        
        if(strpos($asXml, '$XPATH.IN{')) {
            Zend_Debug::dump('$XPATH.IN{}');
            $this->_xpath('IN');
        }
    }
    
    /**
     * Start the xpath process. Here we are trying occurrences with 
     * preg_match_all and it transforms the document.
     *
     * @param string $returnType 'XML' or 'IN'
     */
    private function _xpath($returnType = 'XML') 
    {
        $document = $this->getXmlDocument();
        $asXml = $document->asXml();
        
        $out = array();
        preg_match_all('#\$XPATH\.\\'.$returnType.'\{(.*)\}#', $asXml, $out, PREG_PATTERN_ORDER);
        
        foreach($out[0] as $id => $name) {
            $mask  = $out[1][$id];
            $xpath = $document->xpath($mask);
            $xml   = $this->_xpathToXml($xpath, $returnType);
            $asXml = str_replace($name, $xml, $asXml);
        }
        
        $this->setXmlDocument($asXml);
    }
    
    /**
     * A Xpath request usually a reference array() containing 
     * one or more elements SimpleXMLElement.
     * 
     * Here, we make a conversion to XML element.
     * 
     * If the type is 'XML', it returns the whole element with asXml ().
     * If the type is' IN 'on the reference node child in the form of string.
     *
     * @param unknown_type $xpath
     * @param unknown_type $returnType
     * @return unknown
     */
    private function _xpathToXml($xpath, $returnType) 
    {
        foreach ($xpath as $value) {
            if($returnType === 'XML') {
                return $value->asXml();
            } elseif($returnType === 'IN') {
                return trim((string) $value);
            } else {
                return $value->asXml();
            }
        }
        return null;
    }
}
